home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / clientactive.c < prev    next >
C/C++ Source or Header  |  1993-01-29  |  3KB  |  134 lines

  1. /*  $Revision: 1.9 $
  2. **
  3. */
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include "configdata.h"
  8. #include "paths.h"
  9. #include "clibrary.h"
  10. #include "libinn.h"
  11. #include "nntp.h"
  12. #include "macros.h"
  13.  
  14.  
  15. STATIC char    CApathname[sizeof _PATH_TEMPACTIVE];
  16. STATIC FILE    *CAfp;
  17.  
  18.  
  19. /*
  20. **  Get a copy of the active file for a client host to use, locally or
  21. **  remotely.
  22. */
  23. FILE *
  24. CAopen(FromServer, ToServer)
  25.     FILE    *FromServer;
  26.     FILE    *ToServer;
  27. {
  28.     /* Use a local (or NFS-mounted) copy if available.  Make sure we don't
  29.      * try to delete it when we close it. */
  30.     if ((CAfp = fopen(_PATH_CLIENTACTIVE, "r")) != NULL) {
  31.     CApathname[0] = '\0';
  32.     return CAfp;
  33.     }
  34.  
  35.     /* Use the active file from the server */
  36.     return CAlistopen(FromServer, ToServer, (char *)NULL);
  37. }
  38.  
  39.  
  40. /*
  41. **  Internal library routine.
  42. */
  43. FILE *
  44. CA_listopen(pathname, FromServer, ToServer, request)
  45.     char    *pathname;
  46.     FILE    *FromServer;
  47.     FILE    *ToServer;
  48.     char    *request;
  49. {
  50.     char    buff[BUFSIZ];
  51.     char    *p;
  52.     int        oerrno;
  53.     FILE    *F;
  54.  
  55.     (void)unlink(pathname);
  56.     if ((F = fopen(pathname, "w")) == NULL)
  57.     return NULL;
  58.  
  59.     /* Send a LIST command to and capture the output. */
  60.     if (request == NULL)
  61.     (void)fprintf(ToServer, "list\r\n");
  62.     else
  63.     (void)fprintf(ToServer, "list %s\r\n", request);
  64.     (void)fflush(ToServer);
  65.  
  66.     /* Get the server's reply to our command. */
  67.     if (fgets(buff, sizeof buff, FromServer) == NULL
  68.      || !EQn(buff, NNTP_LIST_FOLLOWS, STRLEN(NNTP_LIST_FOLLOWS))) {
  69.     oerrno = errno;
  70.     CAclose();
  71.     errno = oerrno;
  72.     return NULL;
  73.     }
  74.  
  75.     /* Slurp up the rest of the response. */
  76.     while (fgets(buff, sizeof buff, FromServer) != NULL) {
  77.     if ((p = strchr(buff, '\r')) != NULL)
  78.         *p = '\0';
  79.     if ((p = strchr(buff, '\n')) != NULL)
  80.         *p = '\0';
  81.     if (buff[0] == '.' && buff[1] == '\0') {
  82.         if (ferror(F) || fflush(F) == EOF || fclose(F) == EOF)
  83.         break;
  84.         return fopen(pathname, "r");
  85.     }
  86.     (void)fprintf(F, "%s\n", buff);
  87.     }
  88.  
  89.     /* Ran out of input before finding the terminator; quit. */
  90.     oerrno = errno;
  91.     (void)fclose(F);
  92.     CAclose();
  93.     errno = oerrno;
  94.     return NULL;
  95. }
  96.  
  97.  
  98. /*
  99. **  Use the NNTP list command to get a file from a server.  Default is
  100. **  the active file, otherwise ask for whatever is in the request param.
  101. */
  102. FILE *
  103. CAlistopen(FromServer, ToServer, request)
  104.     FILE    *FromServer;
  105.     FILE    *ToServer;
  106.     char    *request;
  107. {
  108.     /* Gotta talk to the server -- see if we can. */
  109.     if (FromServer == NULL || ToServer == NULL)
  110.     return NULL;
  111.  
  112.     (void)strcpy(CApathname, _PATH_TEMPACTIVE);
  113.     (void)mktemp(CApathname);
  114.     return CAfp = CA_listopen(CApathname, FromServer, ToServer, request);
  115. }
  116.  
  117.  
  118.  
  119. /*
  120. **  Close the file opened by CAopen or CAlistopen.
  121. */
  122. void
  123. CAclose()
  124. {
  125.     if (CAfp) {
  126.     (void)fclose(CAfp);
  127.     CAfp = NULL;
  128.     }
  129.     if (CApathname[0]) {
  130.     (void)unlink(CApathname);
  131.     CApathname[0] = '\0';
  132.     }
  133. }
  134.